/**
 * com.chaeron.GPS - An API for access to GPS devices.
 *
 *
 * Copyright (c) 2001, 2002 Chaeron Corporation,
 * All rights reserved.
 * 
 * Chaeron grants you a non-exclusive license to use, modify and re-distribute
 * this program under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version, provided that this copyright notice and 
 * license appear on all copies of the software.
 * 
 * Software is provided "AS IS," without a warranty of any kind. ALL EXPRESS OR
 * IMPLIED REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
 * HEREBY EXCLUDED. THE ENTIRE RISK ARISING OUT OF USING THE SOFTWARE IS ASSUMED
 * BY THE LICENSEE. See the GNU General Public License for more details.
 *
 * CHAERON AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
 * LICENSEE OR ANY THIRD PARTY AS A RESULT OF USING OR DISTRIBUTING SOFTWARE.
 * IN NO EVENT WILL CHAERON OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE,
 * PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL
 * OR PUNITIVE DAMAGES, HOWEVER CAUSED AN REGARDLESS OF THE THEORY OF LIABILITY,
 * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF CHAERON HAS
 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

 * @version     1.02
 * @author      Andrzej Jan Taramina, Chaeron Corporation
 *              andrzej@chaeron.com
 */

package com.chaeron.GPS.NMEA;

//***************************************************************************
//*
//*     Imports
//*
//***************************************************************************/

import com.chaeron.GPS.*;
import com.chaeron.GPS.event.*;
import com.chaeron.GPS.exception.*;


//***************************************************************************
//*
//*     RMBSentence Class definition
//*
//***************************************************************************/

/**
 * The RMBSentence class represents NMEA RMB - Recommended Minimum Navigation Info "B"
 *
 */

public class RMBSentence extends NMEASentence implements GPSNavDestination
{
	//***************************************************************************
	//*
	//*     Constant definitions
	//*
	//***************************************************************************/
	
	private final static int		RMB_NUM_FIELDS				= 13;
	
	
	//***************************************************************************
	//*
	//*     Attributes
	//*
	//***************************************************************************/
	
	private char		status						= 0;
	private float		crossTrackError				= 0.0f;
	private char		directionToSteer			= 0;
	private String		destination					= "";
	private String		origin						= "";
	private float		latitude					= 0.0f;
	private float		longitude					= 0.0f;
	private float		range						= 0.0f;
	private float		bearing						= 0.0f;
	private float		velocity					= 0.0f;
	private char		arrivalStatus				= 0;
	
	
	//***************************************************************************
	//*
	//*     Constructors
	//*
	//***************************************************************************/
	
	/**
	 * Create a RMBSentence from the given packet data
	 *
	 * @param	tid			talker ID
	 * @param	sid			Sentence ID
	 * @param	sentenceStr	sentence string buffer
	 */
	
	RMBSentence( int tid, int sid, String sentenceStr )
	{  
		super( tid, sid, sentenceStr );
		
		setFieldCount( RMB_NUM_FIELDS );
	}
	
	
	//***************************************************************************
	//*
	//*     Methods
	//*
	//***************************************************************************/
	
	/**
	 * Decode the data fields for a RMB sentence
	 *
	 * @return		This method will return a standard error code or GPS.ERROR_OK
	 * @throws GPSPacketException
	 * @throws GPSFunctionUnavailableException
	 */
	
	public void parseData() throws GPSPacketException, GPSFunctionUnavailableException
	{  
		int		i;
		
		if( getFieldCount() >= RMB_NUM_FIELDS ) {
			status				= getNMEAChar( getField( 0 ) );						// Status
			crossTrackError 	= getNMEAFloat( getField( 1 ) );					// Cross Track Error (nautical miles)
			directionToSteer	= getNMEAChar( getField( 2 ) );						// Direction to Steer (R or L)
			destination			= getField( 3 );									// Destination Waypoint
			origin				= getField( 4 );									// Origin Waypoint
			latitude			= getNMEALatLong( getField( 5 ), getField( 6 ) );	// Destination Waypoint Latitude
			longitude			= getNMEALatLong( getField( 7 ), getField( 8 ) );	// Destination Waypoint Longitude
			range				= getNMEAFloat( getField( 9 ) );					// Range to Destination (nautical miles)
			bearing				= getNMEAFloat( getField( 10 ) );					// Bearing to Destination (degrees true)
			velocity			= getNMEAFloat( getField( 11 ) );					// Destination closing velocity (knots)		
			arrivalStatus		= getNMEAChar( getField( 12 ) );					// Arrival Status
		} else {
			throw( new GPSPacketException( GPSPacketException.EXCEPTION_NUMFIELDS_INVALID ) );
		}
	}
	
	
	//***************************************************************************
	//*
	//*     Sentence Processing Methods
	//*
	//***************************************************************************/
	
	/**
	 * Process the sentence
	 *
	 * @param	gps			the GPS object that requested the RMB sentence to be processed
	 */
	
	void process( GPS gps )
	{
		gps.postGPSEvent( new GPSEvent( GPSEvent.GPS_EVENT_NAVIGATION_DESTINATION, this ) );
	}
	
	
	/**
	 * Get events generated.
	 *
	 * @return		return a valid event type(s) that will be generated by this sentence subclass
	 */
	
	static int getEventsGenerated()
	{  
		return( GPSEvent.GPS_EVENT_NAVIGATION_DESTINATION | GPSEvent.GPS_EVENT_NMEA_SENTENCE );
	}
	
	
	
	/**
	 * Does the object contain valid data?
	 *
	 * @return		Does the object contain valid data?
	 */
	
	public boolean isDataValid()
	{  
		return( crossTrackError != 0.0f || destination.length() > 0 || origin.length() > 0 || latitude != 0.0f || longitude != 0.0f );
	}
	
	
	//***************************************************************************
	//*
	//*    Accessor methods
	//*
	//***************************************************************************/
	
	/**
	 * Get the Status
	 *
	 * @return		status
	 */
	
	public char getStatus()
	{  
		return( status );
	}
	
	
	/**
	 * Get the Cross Track Error (nautical miles)
	 *
	 * @return		Cross Track Error (nautical miles)
	 */
	
	public float getCrossTrackError()
	{  
		return( crossTrackError );
	}
	
	
	/**
	 * Get the Direction to Steer (Right or Left)
	 *
	 * @return		Direction to Steer (Right or Left)
	 */
	
	public char getDirectionToSteer()
	{  
		return( directionToSteer );
	}
	
	
	/**
	 * Get the Destination Waypoint
	 *
	 * @return		 Destination Waypoint
	 */
	
	public String getDestinationWaypoint()
	{  
		return( destination );
	}
	
	
	/**
	 * Get the Origin Waypoint
	 *
	 * @return		 Origin Waypoint
	 */
	
	public String getOriginWaypoint()
	{  
		return( origin );
	}
	
	
	/**
	 * Get the Destination Waypoint Latitude (degrees)
	 *
	 * @return		Destination Waypoint Latitude (degrees)
	 */
	
	public float getDestinationWaypointLatitude()
	{  
		return( latitude );
	}
	
	
	/**
	 * Get the Destination Waypoint Longitude (degrees)
	 *
	 * @return		Destination Waypoint Longitude (degrees)
	 */
	
	public float getDestinationWaypointLongitude()
	{  
		return( longitude );
	}
	
	
	/**
	 * Get the Range to destination (nautical miles)
	 *
	 * @return		Range to destination (nautical miles)
	 */
	
	public float getRangeToDestination()
	{  
		return( range );
	}
	
	
	/**
	 * Get the Bearing to destination (degrees true)
	 *
	 * @return		Bearing to destination (degrees true)
	 */
	
	public float getBearingToDestination()
	{  
		return( bearing );
	}
	
	
	/**
	 * Get the Destination closing velocity (knots)
	 *
	 * @return		Destination closing velocity (knots)
	 */
	
	public float getDestinationClosingVelocity()
	{  
		return( velocity );
	}
	
	
	/**
	 * Get the Arrival Status
	 *
	 * @return		arrival status
	 */
	
	public char getArrivalStatus()
	{  
		return( arrivalStatus );
	}
	
}
